package net.xt.testthread;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class TestCyclicBarrier {

    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i <= 5; i++) {
            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    // 调用await方法，将阻塞当前线程，直到所有线程全部执行完毕后再释放，执行后续代码
                    cyclicBarrier.await();

                    System.out.println(Thread.currentThread().getName() + "运行");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

        System.out.println("end");
    }

}
